import { hospitalAreaListApi } from '@/api/pcs/pcsHospital'
import { defaultPageSize } from '@/utils/antdHelper'
import modelExtend from '@/utils/dvaUtil/modelExtend'

export const namespace = 'pcsHospitalArea'

const initialState = {
  hospitalId: undefined,
  searchParams: {},
  pagination: {
    current: 1,
    pageSize: defaultPageSize,
    total: 0,
  },
  pageList: [],
}

export default modelExtend({
  namespace,
  state: initialState,
  matchedActions: async (toAction, { id }) => {
    toAction({ hospitalId: id })
    toAction('getPageList')
  },
  effects: {
    *getPageList({ payload }, { call, update, select }) {
      const { hospitalId, searchParams, pagination } = yield select(state => state[namespace])
      const params = {
        hospitalId,
        ...searchParams,
        ...pagination,
        ...payload,
      }

      const { data } = yield call(hospitalAreaListApi, params)
      const { content, current, pageSize, total } = data
      yield update({ pageList: content, pagination: { current, pageSize, total } })
    },
  },
})
